package com.luck.easyfoodbatch.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.luck.easyfoodbatch.db.MessageDB;
import com.luck.easyfoodbatch.mvp.mine.MessageModel;
import com.luck.easyfoodbatch.mvp.mine.MessagePresenter;
import com.luck.easyfoodbatch.mvp.mine.MessageView;
import com.luck.easyfoodbatch.mvp.order.MessageM;
import com.luck.easyfoodbatch.util.HttpUtil;

import java.util.List;

/**
 * author：luck
 * project：LoveCare
 * package：com.tongyu.luck.lovecare.service
 * email：893855882@qq.com
 * data：16/8/19
 */
public class MessageService extends Service implements MessageView {
    private MessageModel messageModel;
    private MessageDB messageDB;
    private String message_ids = "";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        messageDB = new MessageDB(this);
        message_ids = messageDB.getMessages();
        if (!TextUtils.isEmpty(message_ids) && message_ids.endsWith(",")) {
            message_ids = message_ids.substring(0, message_ids.length() - 1);
        }
        messageModel = new MessagePresenter(this, this);
        messageModel.getSyncMessage(message_ids);
    }


    @Override
    public void getSyncMessage(MessageM message) {
        if (message.getStatus() == HttpUtil.RESULT_OK) {
            if (message.getData() != null && message.getData().size() > 0) {
                messageDB.saveMessage(message.getData());
            }
        }
    }
}
